package com.lengxf.pay.common.http;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.ContentType;
import com.lengxf.pay.common.bean.Param;
import com.lengxf.pay.common.config.TransactionType;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@Getter
public class HttpParam<P extends Param> {

    private final P param;

    private final TransactionType transactionType;

    private Map<String, String> headerMap;

    private ContentType contentType;

    public HttpParam(P param, TransactionType transactionType) {
        this(param, transactionType, null);
    }

    public HttpParam<P> addHeader(String header, String value) {
        if (headerMap == null) {
            headerMap = new HashMap<>();
        }
        headerMap.put(header, value);
        return this;
    }

    public HttpParam<P> contentType(ContentType contentType) {
        this.contentType = contentType;
        return this;
    }

    public HttpParam(P param, TransactionType transactionType, Map<String, String> headerMap) {
        Assert.notNull(param);
        Assert.notNull(transactionType);
        this.param = param;
        this.transactionType = transactionType;
        this.headerMap = headerMap;
        this.contentType = ContentType.FORM_URLENCODED;
    }

    public String requestUrl() {
        return getTransactionType().serverAddr() + getTransactionType().uri();
    }

}
